package crew.blt.handsound.client.webgl.element;

import org.junit.experimental.categories.Categories.ExcludeCategory;

import com.google.gwt.event.logical.shared.ValueChangeEvent;

import crew.blt.handsound.client.webgl.exception.IllegalSceneStatus;
import crew.blt.handsound.client.webgl.property.Property;
import crew.blt.handsound.client.webgl.property.PropertyFactory;
import crew.blt.handsound.client.webgl.property.PropertyFactory.ExclusiveBooleanProperty;

/**
 * 
 * SceneJS example :
 * <p>
 *	type : "rotate",<br/>
 *	angle:0.0,<br/>
 *	x:1,<br/>
 *	y:1,<br/>
 *	z:0.5
 *
 * @author blt
 *
 */
public class RotateElement extends Element {

	private ExclusiveBooleanProperty exclusivePropertyActivated = null ;
	
	public RotateElement(String id,boolean readValues) throws IllegalSceneStatus {
		super(id);
		addProperty(PropertyFactory.createFloatProperty("angle"),readValues);
		
		addProperty(PropertyFactory.createExclusiveBooleanProperty("x"),readValues);
		addProperty(PropertyFactory.createExclusiveBooleanProperty("y"),readValues);
		addProperty(PropertyFactory.createExclusiveBooleanProperty("z"),readValues);
		
		if(readValues){
			findExclusivePropertyActivated();
		}
			
	}

	private void findExclusivePropertyActivated() throws IllegalSceneStatus {
		boolean foundOne = false;
		for(Property p : getProperties()){
			
			// If this property isn't a ExclusiveBooleanProperty, continue
			if( ! (p instanceof ExclusiveBooleanProperty ) )
				continue;
			
			ExclusiveBooleanProperty exclusiveProperty = (ExclusiveBooleanProperty)p;
			if( exclusiveProperty.getValueWidget().isActivated() ){
				if(foundOne) throw new IllegalSceneStatus("A rotate element should have only one axe, element : "+ getId());
				setExclusivePropertyActivated(exclusiveProperty);
				foundOne = true;
			}
		}
	}

	@Override
	public void onValueChange(ValueChangeEvent event) {
		
		Object o = event.getSource();
		
		// If it's a radio button which had been fired, fire the value change on each of this element.
		if( o instanceof ExclusiveBooleanProperty ){
			setExclusivePropertyActivated((ExclusiveBooleanProperty) o);
		} 
		// Else, it's the angle value so call the regular handler.
		else {
			super.onValueChange(event);
		}
	}

	@Override
	public String getElementType() {
		return "rotate";
	}
	
	public ExclusiveBooleanProperty getExclusivePropertyActivated() {
		return exclusivePropertyActivated;
	}

	public void setExclusivePropertyActivated(ExclusiveBooleanProperty exclusivePropertyActivated) {
		if( getExclusivePropertyActivated() == null ){
			this.exclusivePropertyActivated = exclusivePropertyActivated;
		} else if( ! exclusivePropertyActivated.equals(getExclusivePropertyActivated()) ){
			getExclusivePropertyActivated().setValueByDefault(true);
			this.exclusivePropertyActivated = exclusivePropertyActivated;
		}
		

//		final String propertyNameEvent = ((ExclusiveBooleanProperty) o).getPropertyName();
//		for(Property prop : getProperties()){
//			// If this iteration is not the angle property and not the event's property
//			// set this iteration as not toggled
//			if( ! prop.getPropertyName().equals("angle") &&
//				! propertyNameEvent.equals(prop.getPropertyName()) )
//				prop.setValue(0f, true);
//		}
	}
}
